package com.white.note;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.white.note.base.BaseActivity;
import com.white.note.db.NoteDbHelper;
import com.white.note.entity.NoteInfo;
import com.white.note.entity.UserInfo;

public class CreateNoteActivity extends BaseActivity {

    private EditText et_note_title;
    private EditText et_note_content;
    private Toolbar toolbar;
    private Button btn_submit;
    private NoteInfo noteInfo;

    @Override
    protected int getLayoutResid() {
        return R.layout.activity_create_note;
    }

    @Override
    protected void initViews() {
        et_note_title = findViewById(R.id.et_note_title);
        et_note_content = findViewById(R.id.et_note_content);
        toolbar = findViewById(R.id.toolbar);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void setListener() {
        // 返回
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 点击事件
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = et_note_title.getText().toString();
                String content = et_note_content.getText().toString();
                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
                    Toast.makeText(CreateNoteActivity.this, "请完善信息~~", Toast.LENGTH_SHORT).show();
                } else {
                    UserInfo userInfo = UserInfo.getsUserInfo();
                    if (noteInfo == null) {
                        int row = NoteDbHelper.getInstance(CreateNoteActivity.this).createNote(userInfo.getUsername(), title, content);
                        if (row > 0) {
                            Toast.makeText(CreateNoteActivity.this, "创建成功~~", Toast.LENGTH_SHORT).show();
                            setResult(1000);
                        } else {
                            Toast.makeText(CreateNoteActivity.this, "创建失败~~", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        int row = NoteDbHelper.getInstance(CreateNoteActivity.this).editNote(noteInfo.getNote_id(), title, content);
                        if (row > 0) {
                            Toast.makeText(CreateNoteActivity.this, "修改成功~~", Toast.LENGTH_SHORT).show();
                            setResult(1000);

                        } else {
                            Toast.makeText(CreateNoteActivity.this, "修改失败~~", Toast.LENGTH_SHORT).show();
                        }

                    }
                    finish();

                }
            }
        });
    }

    @Override
    protected void initData() {
        // 跳转传值
        noteInfo = (NoteInfo) getIntent().getSerializableExtra("noteInfo");
        if (noteInfo == null) {
            toolbar.setTitle("新建备忘录");
            btn_submit.setText("新建");
        } else {
            toolbar.setTitle("编辑备忘录");
            btn_submit.setText("编辑");

            et_note_title.setText(noteInfo.getNote_title());
            et_note_content.setText(noteInfo.getNote_content());
        }

    }
}